package com.thy.utils;

import com.github.houbb.sensitive.word.api.IWordAllow;
import com.github.houbb.sensitive.word.api.IWordDeny;
import com.github.houbb.sensitive.word.bs.SensitiveWordBs;
import com.github.houbb.sensitive.word.support.allow.WordAllows;
import com.github.houbb.sensitive.word.support.check.WordChecks;
import com.github.houbb.sensitive.word.support.deny.WordDenys;
import com.github.houbb.sensitive.word.support.ignore.SensitiveWordCharIgnores;
import com.github.houbb.sensitive.word.support.resultcondition.WordResultConditions;
import com.github.houbb.sensitive.word.support.tag.WordTags;

public class SensitiveWordChecker {
    // 全局单例过滤器（确保初始化一次，避免重复加载内置词库）
    private static final SensitiveWordBs SENSITIVE_WORD_BS;
    static {
        //合并默认词库和自定义词库
        IWordDeny wordDeny = WordDenys.chains(WordDenys.defaults(), new BlackWord());
        IWordAllow wordAllow = WordAllows.chains(WordAllows.defaults(), new WhiteWord());
        SENSITIVE_WORD_BS = SensitiveWordBs.newInstance()
                .wordAllow(wordAllow)// 白名单
                .wordDeny(wordDeny)// 黑名单
                .ignoreCase(true)
                .ignoreWidth(true)
                .ignoreNumStyle(true)
                .ignoreChineseStyle(true)
                .ignoreEnglishStyle(true)
                .ignoreRepeat(false)
                .enableNumCheck(false)
                .enableEmailCheck(false)
                .enableUrlCheck(false)
                .enableIpv4Check(false)
                .enableWordCheck(true)
                .wordFailFast(true)
                .wordCheckNum(WordChecks.num())
                .wordCheckEmail(WordChecks.email())
                .wordCheckUrl(WordChecks.url())
                .wordCheckIpv4(WordChecks.ipv4())
                .wordCheckWord(WordChecks.word())
                .numCheckLen(8)
                .wordTag(WordTags.none())
                .charIgnore(SensitiveWordCharIgnores.defaults())
                .wordResultCondition(WordResultConditions.alwaysTrue())
                .init();
    }
    /**
     * 检测内容是否包含敏感词
     * @param content 内容
     * @return 是否包含
     */
    public static boolean check(String content) {
        if (content == null || content.trim().isEmpty()) {
            return false;
        }
        return SENSITIVE_WORD_BS.contains(content);
    }
}
